package com.le.tester;

import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import cn.smallbun.screw.core.process.ProcessConfig;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * currentTime:2022/3/4 10:11
 * description:使用screw生成数据库表结构信息
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class ScrewDBStructureGen {

    private static final Logger LOG = LoggerFactory.getLogger(ScrewDBStructureGen.class);

    @Resource
    public ApplicationContext context;

    //生成数据信息
    @Test
    public void run() {
        try {
            //DataSource dataSource = context.getBean(DataSource.class);
            HikariConfig hikariConfig = new HikariConfig();
            hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
            hikariConfig.setJdbcUrl("jdbc:mysql://112.74.75.226:3306/bojue_applets_0?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai");
            hikariConfig.setUsername("bojue");
            hikariConfig.setPassword("rytjMxMwRPs7sPTT");
            //设置可以获取tables remarks信息
            hikariConfig.addDataSourceProperty("useInformationSchema", "true");
            hikariConfig.setMinimumIdle(2);
            hikariConfig.setMaximumPoolSize(5);
            //hikariConfig.setSchema("bojue_applets_0");
            DataSource dataSource = new HikariDataSource(hikariConfig);

            EngineConfig engineConfig = EngineConfig.builder()
                    //生成的文件路径
                    .fileOutputDir("F:/")
                    .openOutputDir(false)
                    //输出的文件类型
                    .fileType(EngineFileType.WORD)
                    .produceType(EngineTemplateType.freemarker).build();

            Configuration configuration = Configuration.builder()
                    .version("1.0.0")
                    .description("BoJuePhotosDBInfo")
                    .dataSource(dataSource)
                    .engineConfig(engineConfig)
                    .produceConfig(getProcessConfig())
                    .build();

            new DocumentationExecute(configuration).execute();
        } catch (BeansException e) {
            LOG.info("screw生成数据表结构信息异常:{}", e.getMessage());
        }
    }

    //生成的文件结果信息
//    public void GenConfig() {
//        DataSource dataSource = context.getBean(DataSource.class);
//        EngineConfig engineConfig = EngineConfig.builder()
//                //生成的文件路径
//                .fileName("D:/desktop/BoJuePhotosApplicationInfo/DBInfo")
//                .openOutputDir(true)
//                //输出的文件类型
//                .fileType(EngineFileType.HTML)
//                .produceType(EngineTemplateType.freemarker).build();
//
//        Configuration configuration = Configuration.builder()
//                .version("1.0.0")
//                .description("BoJuePhotosDBInfo")
//                .dataSource(dataSource)
//                .engineConfig(engineConfig)
//                .produceConfig(getProcessConfig())
//                .build();
//
//        new DocumentationExecute(configuration).execute();
//    }

    //getProcessConfig
    private ProcessConfig getProcessConfig() {

// 忽略表名，需要忽略的表将表名放到list中即可
        List<String> ignoreTableName = Arrays.asList("aa", "test_group");
        // 忽略表前缀，如忽略a开头的数据库表
        List<String> ignorePrefix = Arrays.asList("test_", "test_");
        // 忽略表后缀
        List<String> ignoreSuffix = Arrays.asList("_test", "czb_");

        return ProcessConfig.builder()
                //根据名称指定表生成
                .designatedTableName(new ArrayList<>())
                //根据表前缀生成
                .designatedTablePrefix(new ArrayList<>())
                //根据表后缀生成
                .designatedTableSuffix(new ArrayList<>())
                //忽略表名
                .ignoreTableName(ignoreTableName)
                //忽略表前缀
                .ignoreTablePrefix(ignorePrefix)
                //忽略表后缀
                .ignoreTableSuffix(ignoreSuffix)
                .build();
    }


}
